package com.balabala.gym_user.controller.admin;

import com.balabala.exception.CustomerException;
import com.balabala.gym_enum.ExceptionEnum;
import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_user.utils.MinIoUtil;
import com.balabala.gym_user.utils.TencentCOS;
import com.balabala.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author 何涛
 * @Date: 2023/05/02/ 13:46
 * @description 上传相关接口
 */
@RestController
@RequestMapping("/admin/upload")
@Slf4j
@CrossOrigin
@Api(tags = "后台上传图片接口")
public class UploadController {

    public final static List<String> ALLOW_IMAGE= Arrays.asList(".jpg",".png",".jpeg",".gif");
    public final static String IMAGE_BUCKET_NAME="gym-user-image";

    @ApiOperation("使用腾讯云上传图片")
    @PostMapping("/uploadImageByTencentCos")
    public Result uploadImage(@RequestParam MultipartFile file)
    {
        try{
            String uploadFileName = file.getOriginalFilename();
            String extName = uploadFileName.substring(uploadFileName.lastIndexOf("."));
            if(!ALLOW_IMAGE.contains(extName)){
                log.info("上传的文件名不合法，文件名：{}",uploadFileName);
                throw new CustomerException(ExceptionEnum.ILLEGAL_EXTNAME);
            }
            //生成新的文件名
            String picName= UUID.randomUUID().toString().replace("-","")+extName;
            //创建临时文件
            File tempFile = File.createTempFile("image-", picName);
            file.transferTo(tempFile);
            //上传到腾讯云存储
            String fileName = TencentCOS.uploadfile(tempFile, "gym_balabala_image");
            log.info("腾讯云返回的文件名为：{}",fileName);
            StringBuilder url=new StringBuilder("https://balabala-gym-1317504463.cos.ap-chengdu.myqcloud.com/").append(fileName);
            log.info("图片访问地址为：{}",url);
            return Result.ok(ResultEnum.GENERALLY_SUCCESS,url.toString());

        }
        catch (Exception e)
        {
            throw new CustomerException(ExceptionEnum.UPLOAD_IMAGE_FAIL);
        }


    }

    @ApiOperation("使用minio上传图片")
    @PostMapping("/uploadImageByMinio")
    @SneakyThrows(Exception.class)
    public Result uploadImageByMinio(@RequestParam MultipartFile file)
    {

        String uploadFileName = file.getOriginalFilename();
        String extName = uploadFileName.substring(uploadFileName.lastIndexOf("."));
        if(!ALLOW_IMAGE.contains(extName)){
            log.info("上传的文件名不合法，文件名：{}",uploadFileName);
            throw new CustomerException(ExceptionEnum.ILLEGAL_EXTNAME);
        }
        //生成新的文件名
        String picName= UUID.randomUUID().toString().replace("-","")+extName;
        //创建临时文件
        //上传到minio
        //创建存储桶
        MinIoUtil.createBucket(IMAGE_BUCKET_NAME);
        String url=MinIoUtil.upload(IMAGE_BUCKET_NAME,picName,file.getInputStream());
        log.info("图片访问地址为：{}",url);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,url.toString());



    }

}
